package com.windbell.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Application;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;

import com.windbell.R;
import com.windbell.ui.adapter.DesktopAdapter;

/**
 * �˵�VIEW
 * 
 * @author Administrator
 * 
 */
public class Desktop {
	public static int mChooesId = 0; // ѡ�����Ǹ��˵�
	private List<Map<String, Object>> mGroup = new ArrayList<Map<String, Object>>();
	private List<List<Map<String, Object>>> mChild = new ArrayList<List<Map<String, Object>>>();
	onChangeViewListener mOnChangeViewListener;

	private Application mApplication;
	private Context mContext;
	private View mView;
	private ExpandableListView expandableListView;
	private DesktopAdapter desktopAdapter;

	public Desktop(Application application, Context context) {
		mApplication = application;
		mContext = context;
		mView = LayoutInflater.from(context).inflate(R.layout.desktop, null);

		findVeiwById();
		initData();
		addListener();
	}

	private void addListener() {
		expandableListView
				.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
					@Override
					public boolean onChildClick(ExpandableListView parent,
							View v, int groupPosition, int childPosition,
							long id) {

						mChooesId = childPosition;
						desktopAdapter.notifyDataSetChanged();
						if (groupPosition == 0) {
							mOnChangeViewListener.onChangeView(childPosition);
						}
						return false;
					}
				});

		expandableListView
				.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
					@Override
					public boolean onGroupClick(ExpandableListView parent,
							View v, int groupPosition, long id) {
						return true;
					}
				});
	}

	public void getGroup(String groupName, String[] items, int[] icos) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", groupName);
		mGroup.add(map);
		List list = new ArrayList();
		for (int i = 0; i < items.length; i++) {
			Map<String, Object> cmap = new HashMap<String, Object>();
			cmap.put("icon", icos[i]);
			cmap.put("name", items[i]);
			cmap.put("click", false);
			list.add(cmap);
		}
		mChild.add(list);
	}

	private void initData() {
		getGroup("新闻资讯", new String[]{"CSDN","CNBLOGS"}, new int[]{R.drawable.v5_0_1_desktop_list_chat,R.drawable.v5_0_1_desktop_list_chat});
		getGroup("常用操作", new String[]{"收藏","设置"}, new int[]{R.drawable.v5_0_1_desktop_list_page,R.drawable.v5_0_1_desktop_list_settings});
		desktopAdapter = new DesktopAdapter(mContext, mGroup, mChild);
		expandableListView.setAdapter(desktopAdapter);
		// 展开
		for (int i = 0; i < mGroup.size(); i++) {
			expandableListView.expandGroup(i);
		}
	}

	public void findVeiwById() {
		expandableListView = (ExpandableListView) mView
				.findViewById(R.id.desktop_list);
	}

	public View getView() {
		return mView;
	}

	public interface onChangeViewListener {
		public abstract void onChangeView(int arg0);
	}

	public void setOnChangeViewListener(
			onChangeViewListener onChangeViewListener) {
		mOnChangeViewListener = onChangeViewListener;
	}
}
